package org.robot;

import java.util.Map;

import org.ConfigLoad;
import org.game.constant.SessionConstent;
import org.game.netty.oldcoder.other.HuanjieMessageFactory;
import org.game.netty.oldcoder.other.IChannelConstants;
import org.game.netty.session.NettySessionFactory;
import org.game.protobuf.c2s.C2SLoginMsg;
import org.game.protobuf.c2s.C2SLoginMsg.EnterGame;

import com.google.common.collect.Maps;
import com.sojoys.artifact.factory.session.ISession;
import com.sojoys.artifact.manager.JsonManager;
import com.sojoys.artifact.manager.MessageManager;
import com.sojoys.artifact.manager.PropManager;
import com.sojoys.artifact.manager.SessionManager;
import com.sojoys.artifact.tools.ToolMap;
import com.sojoys.artifact.tools.ToolRandom;
import com.xiaoleilu.hutool.http.HttpUtil;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

import io.netty.channel.ChannelFuture;
import io.netty.util.Attribute;

/**
 * Hello world!
 *
 */
public class App {

	static final Log log = LogFactory.get(App.class);

	static final String loginUrl = "http://120.131.3.158:8081/login/account/login";

	static final int onlineCount = 1;

	static {
		SessionManager.me().setFactory(new NettySessionFactory());

		MessageManager.me().setFactory(new HuanjieMessageFactory());

		PropManager.use("app.properties");

		String path = "D:/Work/SVN/dal/config/develop/";
		// 所有配置加载
		try {
			ConfigLoad.loadAllCsv(path);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws InterruptedException {
		for (int i = 1; i <= onlineCount; i++) {
			Thread.sleep(ToolRandom.randInt(100, 1000));
			String json = paramBuild(i);
			Map map = JsonManager.me().getJson().parse(json, Map.class);
			int status = ToolMap.getInt("status", map);
			if (status != 0) {
				System.out.println("登陆服务器错误 = " + status);
				System.exit(0);
				return;
			}

			Map data = ToolMap.getMap("data", map);
			String host = ToolMap.getString("gameServerIp", data);
			int port = ToolMap.getInt("gameServerPort", data);
			String token = ToolMap.getString("token", data);

			// 登陆游戏服务器
			// ThreadUtil.execute(() -> {
			try {
				ChannelFuture socket = EchoClient.connect(host, port);
				log.info("************** 连接成功 ************** {}",i);
				Attribute<ISession> session = socket.channel().attr(IChannelConstants.SESSION);
				ISession isession = session.get();
				if (isession == null) {
					isession = SessionManager.me().createSession(socket.channel());
					session.set(isession);
				}

				C2SLoginMsg.EnterGame.Builder builder = EnterGame.newBuilder();
				builder.setToken(token);
				socket.channel().writeAndFlush(MessageManager.me().create((short) 257, builder.build().toByteArray()))
						.sync();

				isession.setAttr(SessionConstent.ENCRYPTION_KEYS, new int[] { 1, 2, 3, 4, 5, 6, 7, 8 });
				isession.setAttr(SessionConstent.DECRYPTION_KEYS, new int[] { 1, 2, 3, 4, 5, 6, 7, 8 });

			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// });
		}
	}

	/**
	 * 参数封装
	 * 
	 * @param i
	 * @return
	 */
	private static String paramBuild(int i) {
		Map paramMap = Maps.newHashMap();
		paramMap.put("accountId", "robot" + i);
		paramMap.put("password", "robot" + i);
		paramMap.put("token", "NULL");
		paramMap.put("deviceid", "1");
		paramMap.put("deviceName", "1");
		paramMap.put("osVersion", "1");
		paramMap.put("osName", "WINDOWS");
		paramMap.put("version", "9.9.9");
		paramMap.put("sdkVersion", "");
		paramMap.put("sdk", "");
		paramMap.put("channelAppId", 1);
		paramMap.put("channelId", "LOCAL_TEST");
		String json = HttpUtil.get(loginUrl, paramMap);
		return json;
	}

}
